#include "Form1.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

Form1::Form1(void) {
	__conn = new XMPPConnection();
}

Form1::~Form1(void) {
}

bool Form1::Initialize() {
	// Construct an XML form
	Construct(L"IDF_FORM1");

	return true;
}

result Form1::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	// Get a button via resource ID
	__pButtonOk = static_cast<Button *> (GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}

	// Get a button via resource ID
	__pButtonSend= static_cast<Button *> (GetControl(L"IDC_BUTTON_SEND"));
	if (__pButtonSend != null)
	{
		__pButtonSend->SetActionId(ID_BUTTON_SEND);
		__pButtonSend->AddActionEventListener(*this);
	}

	return r;
}

result Form1::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void Form1::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch (actionId) {
	case ID_BUTTON_SEND: {
		AppLog("OK Send is clicked! \n");
		if(__conn->IsConnected()){
			Authentication *auth = new Authentication();
			auth->setUsername("a");
			auth->setPassword("123");
			auth->setDigest("ac123", "b");
			String xml = auth->toXML();
			const char* ptxt = (char*) xml.GetPointer();
			AppLog("The text is %S", ptxt);
			__conn->SendData(auth);
		}
	}
	break;
	case ID_BUTTON_OK: {
		AppLog("OK Button is clicked! \n");
		__conn->CreateSocket();
	}
	break;
	default:
		break;
	}
}

